package com.hzk.config;

import com.hzk.pojo.UserBasic;
import com.hzk.service.IUserBasicService;

import com.hzk.vo.CaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @auoter:jackh
 */
@Component
public class HandlerArgumentResolver implements HandlerMethodArgumentResolver {
    @Autowired
    IUserBasicService iUserBasicService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 先执行完上面这个方法才能执行下面那个
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        if (parameter.getParameterType() == UserBasic.class// 参数类型为UsWerBean
                && parameter.hasParameterAnnotation(CaseUser.class)// 参数标注注解为CaseUser
        ) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param parameter
     * @param mavContainer
     * @param webRequest
     * @param binderFactory
     * @return
     * @throws Exception
     */
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest Request = webRequest.getNativeRequest(HttpServletRequest.class);
        HttpServletResponse Response = webRequest.getNativeResponse(HttpServletResponse.class);
        UserBasic user = UserContext.getUser();
        return user;
    }
}
